import React, {Component} from 'react';
import {BrowserRouter as Router, Route, Link, Switch, Redirect} from 'react-router-dom';
import Layout from 'components/Layout';
import NotFound from 'components/NotFound';
import Welcome from 'components/Welcome';


export default class extends Component {
    render() {
        let context = require.context('./', true, /\/route\.js$/);
        return (
            <Router>
                <Layout>
                    <Switch>
                        <Route exact path={'/'} component={Welcome} />
                        <Redirect from="/index" to="/" />
                        {
                            // 路由去中心化 route.js 必须默认返回 {Route|Array<Route>}
                            context.keys()
                                .map(item => context(item).default)
                                .reduce((last, next) => last.concat(Array.isArray(next) ? next : [next]), [])
                        }
                        <Route component={NotFound} />
                    </Switch>
                </Layout>
            </Router>
        );
    }
}